
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace ScrewTurn.Wiki {

	public partial class LogList : System.Web.UI.UserControl {

		protected void Page_Load(object sender, EventArgs e) {
			PrintLog();
		}

		public void PrintLog() {
			StringBuilder sb = new StringBuilder();

			List<LogEntry> log = Log.ReadEntries();
			sb.Append(@"<table width=""98%"" cellpadding=""2"" cellspacing=""0"">");
			sb.Append(@"<tr><td style=""width: 16px;"">&nbsp;</td><td width=""160""><p><b>");
			sb.Append(Resources.Messages.DateTime);
			sb.Append(@"</b></p></td><td width=""140""><p><b>");
			sb.Append(Resources.Messages.User);
			sb.Append(@"</b></p></td><td><p><b>");
			sb.Append(Resources.Messages.Message);
			sb.Append(@"</b></p></td></tr>");
			sb.Append("</table>");
			sb.Append(@"<div style=""overflow: scroll; height: 400px;""><table width=""98%"" cellpadding=""2"" cellspacing=""0"">");
			int count = 0;
			for(int i = 0; i < log.Count; i++) {
				if(log[i].EntryType == EntryType.General) {
					if(chkViewMessages.Checked) {
						PrintEntry(log[i], count, sb);
						count++;
					}
				}
				else if(log[i].EntryType == EntryType.Warning) {
					if(chkViewWarnings.Checked) {
						PrintEntry(log[i], count, sb);
						count++;
					}
				}
				else if(log[i].EntryType == EntryType.Error) {
					if(chkViewErrors.Checked) {
						PrintEntry(log[i], count, sb);
						count++;
					}
				}
			}
			sb.Append("</table></div>");

			lblLog.Text = sb.ToString();
		}

		private void PrintEntry(LogEntry e, int line, StringBuilder sb) {
			if(line % 2 == 0) {
				sb.Append(@"<tr style=""background-color: #F0F0F0;"">");
			}
			else {
				sb.Append(@"<tr>");
			}
			sb.Append(@"<td valign=""top"" style=""width: 16px;""><p>");
			switch(e.EntryType) {
				case EntryType.General:
					sb.Append(@"<img src=""Images/LogGeneral.png"" alt=""General"" title=""General"" />");
					break;
				case EntryType.Warning:
					sb.Append(@"<img src=""Images/LogWarning.png"" alt=""Warning"" title=""Warning"" />");
					break;
				case EntryType.Error:
					sb.Append(@"<img src=""Images/LogError.png"" alt=""Error"" title=""Error"" />");
					break;
			}
			sb.Append("</p></td>");
			string shift = "";
			if(Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
			sb.Append(@"<td valign=""top"" width=""160""><p>");
			sb.Append(string.Format("{0:" + Settings.DateTimeFormat + "}", Tools.AlignWithPreferences(e.DateTime, shift)));
			sb.Append("</p></td>");
			sb.Append(@"<td valign=""top"" width=""140""><p>");
			sb.Append(Users.Instance.UserLink(e.User));
			sb.Append("</p></td>");
			sb.Append(@"<td valign=""top""><p>");
			sb.Append(e.Message);
			sb.Append("</p></td>");
			sb.Append("</tr>");
		}

		protected void btnClearLog_Click(object sender, EventArgs e) {
			Log.ClearLog();
			PrintLog();
		}

	}

}